//名字超
app.filter('hid1',function(){
	return function (value) {
		if(value.length>6){
			var val = value.substr(0,6);
//			console.log(val)
            return val+'...';
		}else{
			return value;
		}
		
        }
})


app.controller('messageCtrl', function($scope, $http) {
     //地址的三级联动
       ca1('sheng','shi','xian');
       ca1('sheng1','shi1','xian1');
function ca1(id1,id2,id3){
	
	var sheng = document.getElementById(id1);
	var shi = document.getElementById(id2);
	var xian = document.getElementById(id3);
	function shengFun() {
		var l = provinceList.length;
		var shtml = '';
		for (var i = 0; i < l; i++) {
			shtml += '<option value=' + i + '>' + provinceList[i].name + '</option>';
		}
		sheng.innerHTML = shtml;
		cityFun(0);
	}

	function cityFun(n) {
		var city = provinceList[n].cityList;
		var l = city.length;
		var shtml = '';
		for (var i = 0; i < l; i++) {
			shtml += '<option value=' + i + '>' + city[i].name + '</option>';
		}
		shi.innerHTML = shtml;
		xianFun(n, 0);
	}

	function xianFun(n, m) {
		var city = provinceList[n].cityList[m].areaList;
		var l = city.length;
		var shtml = '';
		for (var i = 0; i < l; i++) {
			shtml += '<option>' + city[i] + '</option>';
		}
		xian.innerHTML = shtml;
	}

	//设置默认
	shengFun();
	//点击效果
	sheng.onchange = function() {
		var n = this.selectedIndex;
		cityFun(n);
		shi.onchange = function() {
			var m = this.selectedIndex;
			xianFun(n, m);
		};
	};
}
 
	//切换选中地址
	$scope.Mesfun = function(e) {
			$('.g_message_contC>img').attr('src', '../img/f_myaccount/circle0.png')
			$('.g_message_contC>img').removeClass('g_messageImg')
			$(e.target).attr('src', '../img/f_myaccount/circle1.png')
			$(e.target).addClass('g_messageImg');
		}
		//地址信息修改
		$scope.g_messageBol=false;
	$scope.Mesfun1 = function(e) {
		$scope.g_messageBol=true;
		$scope.id = $scope.Adressdata[e].id
		$scope.MesU =$scope.Adressdata[e].getUser
		$scope.MesA =$scope.Adressdata[e].adress
		$scope.MesT =$scope.Adressdata[e].userTel
	}
	    //地址信息修改完更新
	    $scope.Mesfun1_1 = function() {
		$scope.g_messageBol=false;
		 $http.get('../php/g_message4.php?getUser='+$scope.MesU+'&adress='+$scope.MesA+'&userTel='+$scope.MesT+'&id='+$scope.id ).success(function(data) {
//				$scope.Adressdata.splice(b, 1)
			})
		         
	}
		//地址信息删除
	$scope.Mesfun2 = function(a, b) {
			$http.get('../php/g_message2.php?id=' + a).success(function(data) {
				
				$scope.Adressdata.splice(b, 1)
			})
		}
		//默认地址信息切换
	$scope.Mesfun3= function(a) {
		console.log(111)
		$scope.Adressdata[a].moren = 'true';

		if (a == 0) {
			for (var i = 1; i < $scope.Adressdata.length; i++) {
				$scope.Adressdata[i].moren = '';
			}
		} else {
			for (var i = 0; i < a; i++) {
				$scope.Adressdata[i].moren = '';
			}
			for (var j = a + 1; j < $scope.Adressdata.length; j++) {
				$scope.Adressdata[j].moren = '';
			}
		}
	}

	//展开地址
	$('.g_message_contSS span:eq(0)').on('click', function() {
			$http.get('../php/g_message1.php?tel=' + tel + '&token=1').success(function(data) {
				$scope.Adressdata = data;

			})
		})
	//收起地址
	$('.g_message_contSS span:eq(1)').on('click', function() {
			$http.get('../php/g_message1.php?tel=' + tel + '&token=0').success(function(data) {
				$scope.Adressdata = data;
			})
		})
		//检索数据库查询数据地址
	$scope.bol1 = false;
	var tel = localStorage.getItem('tel')
	$http.get('../php/g_message1.php?tel=' + tel + '&token=0').success(function(data) {
		$scope.Adressdata = data;

		if (data.length == 0) {
			$scope.bol1 = false;
		} else if (data.length >= 0) {
			$scope.bol1 = true;
		}
	})

	//获取商品列表
	$scope.mechant = [{
		name: '联合利华',
	}, {
		name: '丹尼斯',
	}];
	$scope.goosedetail = [];
	//获取本地商品参数
	 var localA=JSON.parse(localStorage.getItem('gouwu1'))
	 var localB=JSON.parse(localStorage.getItem('gouwu2'))
	  console.log(localA,localB)
	   if(localA==null&&localB!=null){
	   	$scope.mechant.splice(0,1)
	   }else if(localA!=null&&localB==null){
	   	$scope.mechant.splice(1,1)
	   }else if(localA==null&&localB==null){
	   	 	$scope.mechant.splice(0,2)
	   }
	  $scope.goosedetail.push(localA)
	  $scope.goosedetail.push(localB)
	 
	  
	  
	//单商家价格合计
	$scope.Mesfun4 = function(a) {
		var total = 0;
//		console.log( $scope.goosedetail)
			for ($scope.i in $scope.goosedetail[a]){
         total+=$scope.goosedetail[a][$scope.i].danjia*$scope.goosedetail[a][$scope.i].num          
			}
			 return total;
	}
     //商家总计合计
       $scope.Mesfun5 =function(){
       	  var total = 0;
       	  for( $scope.i in $scope.goosedetail){
       	  	for($scope.j in $scope.goosedetail[$scope.i]){
       	  		total+=$scope.goosedetail[$scope.i][$scope.j].danjia*$scope.goosedetail[$scope.i][$scope.j].num;	
       	  	}

       	  }
       	 	return total;
       }
      //开票信息切换
       $scope.Mesfun6 = function(a){
       	$('.g_message_comLPiao1Ft1').css('background','url(../img/f_myaccount/bai.png)no-repeat left center');
       	$(a.target).css('background','url(../img/f_myaccount/green.png)no-repeat left center');
       
       }
       //时间列表
       $scope.bolTime = false;
      $('.g_message_comLBomFt4').html('12:00~3:00');
        $('.g_message_comLBiao').on('click',function(e){
        	if(e.target && e.target.nodeName == "TD") {
        		$('.g_message_comLBomFt4').html(e.target.innerHTML);
        		
        	
        		$('.g_message_comLBiao td').css({
		      	color: '#666'
		      })
		      $(e.target).css({
		      	color:'red'
		      })
	        }
        })
       
       //提交订单存入数据库
       $('.g_messagePayFt5').on('click',function(){
         var recievAdress =  $('.g_messageImg').next().text();//地址
         var money = $('.g_messagePayFt2').html();//总价
         var shangjia = $scope.mechant;  //商店数组
         var gooseA =  $scope.goosedetail[0]//商品1数组
          var gooseB =  $scope.goosedetail[1]//商品2数组

         for(var i=0;i<gooseA.length;i++){
         	//A存入数据库
          $http.get('../php/g_message5.php?shangjia='+shangjia[0].name+'&image='+gooseA[i].img+'&name='+gooseA[i].name+'&guige='+gooseA[i].guige+'&num='+gooseA[i].num+'&danjia='+gooseA[i].danjia+'&address='+recievAdress).success(function(data){
//        	console.log(data)	
          })	
         }
         if(gooseB!=null){
          for(var j=0;j<gooseB.length;j++){
         	//B存入数据库
          $http.get('../php/g_message5.php?shangjia='+shangjia[1].name+'&image='+gooseB[j].img+'&name='+gooseB[j].name+'&guige='+gooseB[j].guige+'&num='+gooseB[j].num+'&danjia='+gooseB[j].danjia+'&address='+recievAdress).success(function(data){
          	console.log(data)	
          })
         }
         }
         //存入本地
         localStorage.setItem('address',recievAdress);
          localStorage.setItem('money',money);
          $('.g_messagePayFt5').parent().attr('href', '#/g_suOrder')
       })
       
       
      
        
       



})